package nl.rainbowsheep.reisorg.empl.site.controller;

import java.util.List;
import javax.validation.Valid;
import nl.rainbowsheep.reisorg.empl.site.model.Region;
import nl.rainbowsheep.reisorg.empl.site.service.CityService;
import nl.rainbowsheep.reisorg.empl.site.service.CountryService;
import nl.rainbowsheep.reisorg.empl.site.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/region")
public class RegionController {

    @Autowired
    CountryService countryservice;

    @Autowired
    RegionService regionservice;

    @Autowired
    CityService cityservice;

    @RequestMapping(value = {"/", "/index", ""})
    public ModelAndView getAll() {
        List<Region> regions = regionservice.getAll();
        ModelAndView model = new ModelAndView("region/index");
        model.addObject("regions", regions);
        return model;
    }

    @RequestMapping(value = {"/getbycountryid/{id}"})
    public @ResponseBody List<Region> getAllRegionsByCountryId(@PathVariable long id) {
        List<Region> regions = regionservice.getByCountryId(id);
        return regions;
    }

    @RequestMapping(value = {"/getbycountries"})
    public List<Region> getAllRegions() {
        List<Region> regions = regionservice.getAll();
        return regions;
    }

    @RequestMapping(value = "/details/{id}", method = RequestMethod.GET)
    public ModelAndView getRegion(@PathVariable long id) {
        Region region = regionservice.getById(id);
        ModelAndView mav = new ModelAndView("region/details");
        mav.addObject("region", region);
        return mav;
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView getRegionForm() {
        ModelAndView mav = new ModelAndView("region/create");
        mav.addObject("region", new Region());
        return mav;
    }

    /**
     *
     * @param country
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String createRegion(@ModelAttribute("region") @Valid Region region, BindingResult bindingResult) {
        regionservice.insertRegion(region);
        // moet nog
        return "region/create";
    }
}
